package com.saclub.app.http;

import android.app.Activity;

import com.loopj.android.http.RequestParams;
import com.offroader.http.HttpHelper;
import com.offroader.http.MyJsonHttpResponseHandler;
import com.offroader.utils.LogUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.saclub.app.R;
import com.saclub.app.common.AppConfig;
import com.saclub.app.common.MyPageHelper;
import com.saclub.app.handler.BookMarkAddResponseHandler;
import com.saclub.app.handler.BookMarkListResponseHandler;
import com.saclub.app.handler.ContactWeResponseHandler;
import com.saclub.app.handler.LoginResponseHandler;
import com.saclub.app.handler.MyBaseHttpResponseHandler;
import com.saclub.app.handler.MyBaseHttpResponseHandler.HandlerType;
import com.saclub.app.handler.NewsResponseHandler;
import com.saclub.app.handler.RegResponseHandler;
import com.saclub.app.handler.SectionResponseHandler;
import com.saclub.app.handler.SliderResponseHandler;
import com.saclub.app.handler.UpdateResponseHandler;
import com.saclub.app.handler.WelcomeAdResponseHandler;

/**
 * APP接口请求
 * 
 * @author li.li
 *
 */
public enum ApiRequest {
	welcome_ad("封面图（启动广告）", "/json_cover.aspx", ApiTypeEnum.get, HandlerType.loading, WelcomeAdResponseHandler.class), //
	news("新闻列表", "/json_news.aspx", ApiTypeEnum.get, HandlerType.loading, NewsResponseHandler.class), //
	slider("封面图（轮转图）", "/json_cover.aspx", ApiTypeEnum.get, HandlerType.loading, SliderResponseHandler.class), //
	section("模块分类", "/json_section.aspx", ApiTypeEnum.get, HandlerType.loading, SectionResponseHandler.class), //
	userLogin("登陆", "/userlogin.aspx", ApiTypeEnum.get, HandlerType.loading, LoginResponseHandler.class), //
	userReg("注册", "/userupdate.aspx", ApiTypeEnum.get, HandlerType.loading, RegResponseHandler.class), //
	userUpdate("更新", "/userupdate.aspx", ApiTypeEnum.get, HandlerType.loading, UpdateResponseHandler.class), //
	userLikeList("我的收藏", "/json_bookmarks.aspx", ApiTypeEnum.get, HandlerType.loading, BookMarkListResponseHandler.class), //
	userAddLike("添加收藏", "/bookmark_add.aspx", ApiTypeEnum.get, HandlerType.loading, BookMarkAddResponseHandler.class), //
	userContactWe("联系我们", "/json_bookmarks.aspx", ApiTypeEnum.get, HandlerType.waitting, ContactWeResponseHandler.class), //
	;

	/** 接口名 */
	private String name;
	/** 接口地址 */
	private String apiPath;
	/** 接口类型 */
	private ApiTypeEnum apiType;
	/** 接口处理器 */
	private Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz;
	/** 处理器类型 */
	private HandlerType handlerType;

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, HandlerType handlerType,
			Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
		this.handlerType = handlerType;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType, Class<? extends MyJsonHttpResponseHandler<?, ?>> handlerClazz) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.handlerClazz = handlerClazz;
		this.name = name;
	}

	private ApiRequest(String name, String apiPath, ApiTypeEnum apiType) {
		this.apiPath = apiPath;
		this.apiType = apiType;
		this.name = name;
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType) {
		request(caller, params, handlerType, true);
	}

	public <T> void request(T caller, RequestParams params, HandlerType handlerType, boolean showTip) {
		if (handlerClazz == null)
			return;

		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;
		//		String apiUrl = AppConfig.API_DOMAIN;

		try {
			MyBaseHttpResponseHandler<T, ?> responseHandler = (MyBaseHttpResponseHandler<T, ?>) handlerClazz.newInstance();

			//Handler初始化
			responseHandler.init(caller);

			//设置结果处理类型
			responseHandler.setHandlerType(handlerType);

			//设置为测试模式（读本地接口数据）
			responseHandler.setTest(false);

			//是否显示toast
			responseHandler.setShowTip(showTip);

			LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl + "?" + params.toString());

			//接口类型
			if (ApiTypeEnum.get.equals(apiType))
				HttpHelper.getInstance().get(apiUrl, params, responseHandler);
			else if (ApiTypeEnum.post.equals(apiType))
				HttpHelper.getInstance().post(apiUrl, params, responseHandler);
			else
				LogUtils.error("错误", new Exception("非GET&POST接口禁止调用|" + name));

		} catch (Throwable e) {
			LogUtils.error(e.getMessage(), e);
			ViewUtils.toast(R.string.net_data_error);
		}
	}

	public <T> void request(T caller, RequestParams params) {

		request(caller, params, handlerType, true);

	}

	public <T> void request(T caller, RequestParams params, boolean showTip) {

		request(caller, params, handlerType, showTip);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, String title) {

		RequestParams params = new RequestParams();
		request(caller, params);

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 * @param title
	 */
	public void request(Activity caller, RequestParams params, String title) {
		String apiUrl = AppConfig.API_DOMAIN + AppConfig.API_PATH + apiPath;

		if (params != null) {

			String p = params.toString();

			if (StringUtils.isNotBlank(p)) {
				apiUrl = apiUrl + "?" + p;
			}
		}

		LogUtils.info("接口请求|" + apiType.name() + "|" + apiUrl);

		if (ApiTypeEnum.h5.equals(apiType))
			MyPageHelper.webView.showMyWebViewPage(caller, apiUrl, title);
		else
			LogUtils.error("错误", new Exception("非H5接口禁止调用|" + name));

	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller) {

		request(caller, name);
	}

	/**
	 * H5页面 
	 * @param caller
	 * @param url
	 */
	public void request(Activity caller, RequestParams params) {

		request(caller, params, name);
	}

	/**
	 * 接口类型
	 * @author li.li
	 *
	 */
	private enum ApiTypeEnum {
		get, //获取数据
		post, //发送数据
		h5, //H5网址
		;
	}

}
